package com.gopay.scheduler.job;

import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.order.MainOrderField;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.OrderLifecycleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransType01813TimeoutCleanJob extends GopayBaseJob{
	public static final String TRANSTYPE = "01813";
	private Logger logger = LoggerFactory
			.getLogger(TransType01813TimeoutCleanJob.class);
	private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;
	private MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient;

	private static final int QUERYCOUNT = 5000;

	@Resource(name = "cpsGeneralMainOrderOperClient")
	public void setCpsGeneralMainOrderOperClient(
			MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient) {
		this.cpsGeneralMainOrderOperClient = cpsGeneralMainOrderOperClient;
	}

	@Resource(name = "cpsGeneralMainOrderDAO")
	public void setCpsGeneralMainOrderDAO(
			CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO) {
		this.cpsGeneralMainOrderDAO = cpsGeneralMainOrderDAO;
	}

	@Override
	protected void doJobInternal() {
		try {
			Map param = new HashMap();
			String B2bName = TRANSTYPE + OrderLifecycleUtil.B2B;
			String B2cName = TRANSTYPE + OrderLifecycleUtil.B2C;
			String b2c = OrderLifecycleUtil.getLifecycle(B2cName);
			String b2b = OrderLifecycleUtil.getLifecycle(B2bName);
			param.put("b2b", b2b);
			param.put("b2c", b2c);
			long startMillions = System.currentTimeMillis();
			logger.info("查询超时订单执行开始 ");
			List<Object> orderList = cpsGeneralMainOrderDAO.findTimeoutOrdersInOrder01813(param, 1, QUERYCOUNT);
			updateStatus(orderList);
			long endMiilions = System.currentTimeMillis();
			long result = (endMiilions - startMillions) / 1000;
			logger.info("超时订单执行结束 耗费=" + result + "秒");
		} catch (Exception e) {
			logger.error("处理失败！",e);
		}
	}
	private void updateStatus(List<Object> orderList){
		if (!(orderList.isEmpty())) {
			String txnStaCode = String
					.valueOf(TxnStaCode.TXN_STA_CD_20002.value);
			logger.info("01813 timeout orders Id:"+orderList.toString());
			logger.info("found"
					+ orderList.size()
					+ "other orders  records, overtime timeout order clean-up work now!");

			Map<MainOrderField, String> map = new HashMap<MainOrderField, String>();

			map.put(MainOrderField.TXN_STA_CD, txnStaCode);
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
			map.put(MainOrderField.TRAN_FINISH_TIME, sf.format(new Date()));
			for (Object object : orderList) {

				String gopayOrderId = String.valueOf(object);

				updateOrderStatusFailure(gopayOrderId,map);
			}
		}
	}

	@Transactional
	private void updateOrderStatusFailure(String gopayOrderId,Map<MainOrderField, String> map) {
		try {
			cpsGeneralMainOrderOperClient.updateMany(gopayOrderId, map);
			//由于01813云账户子账户收款有特殊的业务需要关联订单
			CpsGenMainOrder order00901 = cpsGeneralMainOrderDAO.findByRelevanceAndTxnCd(gopayOrderId, IntTxnCd._00901.value);
			cpsGeneralMainOrderOperClient.updateMany(order00901.getGopayOrderId(), map);

			CpsGenMainOrder order01812 = cpsGeneralMainOrderDAO.findByRelevanceAndTxnCd(gopayOrderId, IntTxnCd._01812.value);
			cpsGeneralMainOrderOperClient.updateMany(order01812.getGopayOrderId(), map);

		} catch (GopayException e) {
			logger.error("update order fail,orderId:" + gopayOrderId,e);
		} catch (Exception e) {
			logger.error("update order fail,orderId:" + gopayOrderId,e);

		}
	}

}
